
% A1a_extract_data_entry
%==========================================================================

% Description: This code extracts price indexes and income draws from the
% main supply side estimation and the entry-only case


clear
clc

cd 'D:\data_replication'

P_summary = [];

load('estimation/5_supply_side/main_data_supply');

for i_k = 1:4092

i_k
    
k = index_price_coeff(i_k,1);    


data_k = data_summary{k};
numProdsTotal = size(data_k,1);
X_MPEC_k = X_MPEC_summary{k};
X_MPEC_opt = X_MPEC_opt_summary{k};
share_k = data_k(:,5);
price_k = data_k(:,7) + 1;
gdp_capita_k = data_k(:,13);
distance_k = data_k(:,14);
population_k = data_k(:,16);
population_log_k = log(population_k);
Y_k = Y_summary{k};
FE_k = FE_summary{k};
market_id_k = data_k(:,end);
T = market_id_k(end,1);
nn = size(Y_k,2);
alpha_constant = X_MPEC_opt(1,1);
alpha_distance = X_MPEC_opt(2,1);
alpha_population = X_MPEC_opt(3,1);
alpha_price = X_MPEC_opt(4,1);
alpha_1 = X_MPEC_opt(12,1);
alpha_FE = X_MPEC_opt((3*(K_A+1) + 1):(3*(K_A+1) + (K - K_A)), 1);

xi_k = X_MPEC_opt((3*(K_A+1) + (K - K_A) + 2 + 1):(3*(K_A+1) + (K - K_A) + 2 + numProdsTotal), 1) + 10;     % Ensure positive values
xi_k = xi_k - alpha_constant - log(price_k)*alpha_price - alpha_population*population_log_k - alpha_distance*distance_k;
q_k = alpha_constant + xi_k + FE_k*alpha_FE;


prodsMarket = zeros(T,1);
prodsMarket_temp = data_k(:,17);
for t = 1:T
prodsMarket_temp2 = prodsMarket_temp(market_id_k==t);
prodsMarket(t,1) = prodsMarket_temp2(1,1);
end

marketStarts = zeros(T,1);
marketEnds = zeros(T,1);
marketStarts(1) = 1;
marketEnds(1) = prodsMarket(1);
for t=2:T
    marketStarts(t) = marketEnds(t-1) + 1;
    marketEnds(t) = marketStarts(t) + prodsMarket(t) - 1;
end;

marketForProducts = zeros(numProdsTotal,1);
for t=1:T
marketForProducts(marketStarts(t):marketEnds(t)) = t;
end
numProdsTotal = size(data_k, 1);


delta_k =  alpha_constant + xi_k + log(price_k)*alpha_price + alpha_population*population_log_k + alpha_distance*distance_k;

for t = 1:T   
Y_market = Y_k(t,:);
expmu(marketStarts(t):marketEnds(t),:) = exp(log(price_k(marketStarts(t):marketEnds(t),:))*alpha_1*Y_market + repmat(FE_k(marketStarts(t):marketEnds(t),:)*alpha_FE,1,nn));
mu_true(marketStarts(t):marketEnds(t),:)= log(price_k(marketStarts(t):marketEnds(t),:))*alpha_1*Y_market + repmat(FE_k(marketStarts(t):marketEnds(t),:)*alpha_FE,1,nn);
end
expmeanval = exp(delta_k);
meanval_true = delta_k;
oo = ones(1,nn);                  
sharesum = sparse(zeros(T,numProdsTotal));  % used to create denominators in logit predicted shares (i.e. sums numerators)
for t = 1:T
    sharesum(t,marketStarts(t):marketEnds(t)) = 1;
end
numer = (expmeanval*oo ).*expmu;  
v_true = (meanval_true*oo ) + mu_true; 
sum1 = sharesum*numer;
sum11 = 1./(0+sum1);                                                       % Will not result in share_k = EstShare_true
denom1 = sum11(marketForProducts,:);    
simShare_true = numer.*denom1;              
EstShare_true = mean(simShare_true,2);  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%
% Compute Utilities %
%%%%%%%%%%%%%%%%%%%%%

% TRUE UTILITIES

filename_costs = sprintf('robustness/nested_CES/cost_estimates/costs_%i_k.mat', i_k);  
load(filename_costs, 'mc_opt', 'f_opt', 'mc_opt_summary')
N_true = (price_k - mc_opt) ./ price_k .* EstShare_true ./ f_opt;
N_counter = (price_k - mc_opt) ./ price_k  ./ f_opt;
numer_counter = numer .* repmat(N_counter, 1, nn) ./ repmat(N_true, 1, nn);
v_counter = log(numer_counter); 



P_true_k = zeros(T,nn);
beta_price = Y_k * alpha_1 + alpha_price;
for tt = 1:T
v_true_t = v_true(marketStarts(tt,1):marketEnds(tt,1),:,:); 
beta_price_t = beta_price(tt, :);
exp_v_true_t = exp(v_true_t);
s_exp_v_true_t = sum(exp_v_true_t);
P_true_k(tt,:) = s_exp_v_true_t .^ (1 ./ (beta_price_t));
end


% COUNTERFACTUAL UTILITIES

P_counter_k = zeros(T,nn);
beta_price = Y_k * alpha_1 + alpha_price;
for tt = 1:T
data_k_t = data_k(marketStarts(tt,1):marketEnds(tt,1),:);   
index_domestic = data_k_t(:,3) == data_k_t(:,4);
v_counter_t = v_counter(marketStarts(tt,1):marketEnds(tt,1),:,:); 
v_counter_t = v_counter_t(index_domestic, :);
beta_price_t = beta_price(tt, :);
exp_v_counter_t = exp(v_counter_t);
s_exp_v_counter_t = exp_v_counter_t;
if sum(index_domestic) == 1
P_counter_k(tt,:) = s_exp_v_counter_t .^ (1 ./ (beta_price_t));
end
if sum(index_domestic) == 0 
P_counter_k(tt,:) = exp(1) * ones(1, nn);
end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute Price Indexes %
%%%%%%%%%%%%%%%%%%%%%%%%%

beta_price = Y_k * alpha_1 + alpha_price;

ratio = P_counter_k ./ P_true_k;


ratio_sorted = zeros(T, nn);
Y_k_sorted = zeros(T, nn);
P_true_k_sorted = zeros(T, nn);
P_counter_k_sorted = zeros(T, nn);
beta_price_sorted = zeros(T, nn);
for tt = 1:T
[Y_k_tt, ind] = sort(Y_k(tt,:));
Y_k_sorted(tt,:) = Y_k_tt;
P_true_k_sorted(tt,:) = P_true_k(tt,ind);
P_counter_k_sorted(tt,:) = P_counter_k(tt,ind);
ratio_sorted(tt,:) = ratio(tt,ind);
beta_price_sorted(tt,:) = beta_price(tt,ind);
end


diff = ratio_sorted(:,20) - ratio_sorted(:,80);
P_true_k_sorted_20 = P_true_k_sorted(:,20);
P_true_k_sorted_80 = P_true_k_sorted(:,80);
P_counter_k_sorted_20 = P_counter_k_sorted(:,20);
P_counter_k_sorted_80 = P_counter_k_sorted(:,80);
beta_price_20 = beta_price_sorted(:,20);
beta_price_80 = beta_price_sorted(:,80);
Y_20 = Y_k_sorted(:,20);
Y_80 = Y_k_sorted(:,80);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Prepare Data for Output Files and Save %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


year_t = zeros(T,1);
quarter_t = zeros(T,1);
declarant_t = zeros(T,1);
mc_flag_t = zeros(T,1);
price_domestic = zeros(T,1);
gdp_capita_domestic = zeros(T,1);
domestic_producer = zeros(T,1);
for tt = 1:T
data_tt = data_k(marketStarts(tt,1):marketEnds(tt,1),:);     
year_t(tt,1) = data_tt(1,1);
quarter_t(tt,1) = data_tt(1,2);
declarant_t(tt,1) = data_tt(1,3);
index_domestic = data_tt(:,3) == data_tt(:,4);
if sum(index_domestic) == 1
price_domestic(tt,1) = data_tt(index_domestic,7);
gdp_capita_domestic(tt,1) = data_tt(index_domestic,13);
domestic_producer(tt,1) = 1;
mc_flag_t(tt,1) = mc_opt_summary(index_domestic, 3);
end
end

data_save = array2table([i_k * ones(T,1), year_t, quarter_t, declarant_t, price_domestic, P_true_k_sorted_20, P_true_k_sorted_80, P_counter_k_sorted_20, P_counter_k_sorted_80, Y_20, Y_80, beta_price_20, beta_price_80, mc_flag_t]);
data_save.Properties.VariableNames = {'i_k', 'year', 'quarter' 'declarant', 'price', 'P_true_k_sorted_20', 'P_true_k_sorted_80', 'P_counter_k_sorted_20', 'P_counter_k_sorted_80', 'Y_20', 'Y_80', 'beta_price_20', 'beta_price_80', 'mc_flag'};  

P_summary = [P_summary; data_save];

clearvars -except P_summary K K_A KK index_price_coeff data_summary X_MPEC_summary X_MPEC_opt_summary Y_summary FE_summary

end

writetable(P_summary, 'robustness/nested_CES/P_summary.csv', 'Delimiter',',','QuoteStrings',true);

save('robustness/nested_CES/P_summary.mat', 'P_summary');

